package by.hqsoftware.RollingBall;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

import android.os.Bundle;
import android.util.DisplayMetrics;

public class RollingBall extends Activity implements SensorEventListener {
	
	private SensorManager mSensorManager;
	private Sensor mAccelerometer;
	private GameView mGameView;	
	
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        mGameView = new GameView(this);        
        setContentView(mGameView);
        
        //get sensor manager
        mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
        //get sensor
        mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        //get game view        

        //get metrics
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);        
        
        //calculate 
        mGameView.metersToPixelsX = metrics.xdpi / 0.0254f;
        mGameView.metersToPixelsY = metrics.ydpi / 0.0254f;
    }
        
    
    @Override
    protected void onResume() {
    	super.onResume();
    	//register accelerometer
    	mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_GAME);
    	//TODO check if not available
    }
    
    @Override
    protected void onPause() {    	
    	super.onPause();
    	//unregister accelerometer
    	mSensorManager.unregisterListener(this, mAccelerometer);
    }

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {		
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		mGameView.UpdateBallsPosition(event);
	}
	
}